package com.ihighest.app.infox.adapter;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.LaunchGroupChatActivity;

/**
 * 成员推荐适配器
 * 
 * @author lkx Created at 2015-10-14 下午3:38:17
 * @version 1.0
 */
public class MembersRecommendedAdapter extends BaseAdapter {
	private final Context context;
	private JSONArray array;
	private final LayoutInflater inflater;
	// protected ImageLoader imageLoader = ImageLoader.getInstance();
	HashMap<Integer, Boolean> state = new HashMap<Integer, Boolean>();

	public MembersRecommendedAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public MembersRecommendedAdapter(Context context, JSONArray jsonArray) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.array = jsonArray;
	}

	@Override
	public int getCount() {
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View contextView, ViewGroup parent) {
		ViewHolder holder;
		if (contextView == null) {
			holder = new ViewHolder();
			contextView = inflater.inflate(R.layout.infox_groupchat_recommended_item, null);
			holder.personIcon = (ImageView) contextView.findViewById(R.id.personIcon);
			holder.name = (TextView) contextView.findViewById(R.id.personName);
			holder.tv_uid = (TextView) contextView.findViewById(R.id.tv_uid);
			holder.tv_rid = (TextView) contextView.findViewById(R.id.tv_rid);
			holder.tv_mid = (TextView) contextView.findViewById(R.id.tv_mid);
			holder.box = (CheckBox) contextView.findViewById(R.id.choosePerson);
			contextView.setTag(holder);
		} else {
			holder = (ViewHolder) contextView.getTag();
		}

		holder.box.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				LaunchGroupChatActivity act = (LaunchGroupChatActivity) context;
				if (arg1) {
					state.put(position, arg1);
					act.memberids.add(arg0.getTag().toString());
				} else {
					state.remove(position);
					act.memberids.remove(arg0.getTag().toString());
				}
			}
		});
		// Resources resources = contextView.getResources();
		// Drawable drawable =
		// resources.getDrawable(R.drawable.infox_person_no_icon);
		// DisplayImageOptions defaultOptions = new
		// DisplayImageOptions.Builder().showImageForEmptyUri(drawable).showImageOnFail(drawable)
		// .showImageOnLoading(drawable).cacheInMemory(true).cacheOnDisc(true).build();
		try {
			JSONObject json = array.getJSONObject(position);
			AppKit.SetUserImg(json.getString("image"), holder.personIcon);
			// imageLoader.displayImage(json.getString("image"),
			// holder.personIcon, defaultOptions);
			holder.tv_uid.setText(json.getString("uid"));
			holder.tv_rid.setText(json.getString("rid"));
			holder.tv_mid.setText(json.getString("mid"));
			holder.name.setText(json.getString("title"));
			holder.box.setTag(json.getString("uid"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		holder.box.setChecked(state.get(position) == null ? false : true);
		return contextView;
	}

	class ViewHolder {
		ImageView personIcon;
		TextView name, tv_uid, tv_rid, tv_mid;
		CheckBox box;
	}
}
